package com.xiaofu.controller;


import com.xiaofu.enums.BizCodeEnum;
import com.xiaofu.request.AddressAddRequest;
import com.xiaofu.service.AddressService;
import com.xiaofu.utils.JsonData;
import com.xiaofu.vo.AddressVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 电商-公司收发货地址表 前端控制器
 * </p>
 *
 * @author 小夫
 * @since 2024-07-01
 */
@RestController
@Api(tags = "收货地址模块")
@RequestMapping("/api/address/v1")
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 根据id查询收货地址详情
     * @param addressId
     * @return
     */
    @GetMapping("find/{address_id}")
    @ApiOperation("根据id查询收货地址详情")
    public JsonData detail(@ApiParam(value = "地址id",readOnly = true) @PathVariable("address_id") Long addressId) {
        AddressVO addressVo = addressService.detail(addressId);
        return addressVo != null ? JsonData.buildSuccess(addressVo) : JsonData.buildResult(BizCodeEnum.ADDRESS_NO_EXITS);
    }

    /**
     * 新增收货地址
     * @param request
     * @return
     */
    @ApiOperation("新增收货地址")
    @PostMapping("add")
    public JsonData add(@ApiParam("地址对象") @RequestBody AddressAddRequest request) {
        int row = addressService.add(request);
        return JsonData.buildSuccess(row);
    }


    /**
     * 删除指定收货地址
     * @param addressId
     * @return
     */
    @DeleteMapping("/del/{address_id}")
    @ApiOperation("删除指定收货地址")
    public JsonData del(@ApiParam(value = "地址ID", required = true) @PathVariable("address_id") int addressId) {
        int rows = addressService.del(addressId);
        return JsonData.buildSuccess(rows);
    }

    /**
     * 查询用户的全部收货地址
     * @return
     */
    @ApiOperation("查询用户的全部收货地址")
    @GetMapping("list")
    public JsonData findUserAllAddresses() {
        List<AddressVO> list = addressService.listUserAllAddresses();
        return JsonData.buildSuccess(list);
    }

}

